using System;
using System.Diagnostics.CodeAnalysis;
using iPhOwa.Exchange.Attributes;
using iPhOwa.Exchange.ContentClasses.Fields;

namespace iPhOwa.Exchange.ContentClasses
{
	/// <summary>
	/// The Exchange message entity.
	/// </summary>
	[ExchangeContentClass(ContentClassNames.Message)]
	public class Message : Item
	{
		private string normalizedSubject;
		private string subject;

		/// <summary>
		/// From line
		/// </summary>
		public override string DisplayFrom
		{
			get { return FromName; }
		}

		/// <summary>
		/// Display date.
		/// </summary>
		public override DateTime DisplayDate
		{
			get { return DateReceived; }
		}

		/// <summary>
		/// The date and time that the message was received by the server.
		/// </summary>
		[ExchangeField(MessageFields.DateReceived)]
		public DateTime DateReceived { get; set; }

		/// <summary>
		/// Friendly display of the message CC line.
		/// </summary>
		[ExchangeField(MessageFields.DisplayCC)]
		public string DisplayCC { get; set; }

		/// <summary>
		/// Friendly display of the message To line.
		/// </summary>
		[ExchangeField(MessageFields.DisplayTo)]
		public override string DisplayTo { get; set; }

		/// <summary>
		/// Message cc line.
		/// </summary>
		[ExchangeField(MessageFields.CC)]
		public string CC { get; set; }

		/// <summary>
		/// Message to line.
		/// </summary>
		[ExchangeField(MessageFields.To)]
		public string To { get; set; }

		/// <summary>
		/// Message from line.
		/// </summary>
		[ExchangeField(MessageFields.From)]
		public string From { get; set; }

		/// <summary>
		/// Whether or not the message flag has been marked completed.
		/// </summary>
		[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flag")]
		[ExchangeField(MessageFields.FlagCompleted)]
		public bool FlagCompleted { get; set; }

		/// <summary>
		/// The e-mail address from which the message was sent.
		/// </summary>
		[ExchangeField(MessageFields.FromEmail)]
		public string FromEmail { get; set; }

		/// <summary>
		/// The friendly display name of the message sender.
		/// </summary>
		[ExchangeField(MessageFields.FromName)]
		public string FromName { get; set; }

		/// <summary>
		/// Indicates whether or not the message has attachments.
		/// </summary>
		[ExchangeField(MessageFields.HasAttachment)]
		public bool HasAttachment { get; set; }

		/// <summary>
		/// The Text content of the message.
		/// </summary>
		[ExchangeField(MessageFields.TextDescription)]
		public string TextDescription { get; set; }

		/// <summary>
		/// Text description of the flag on the message.
		/// </summary>
		[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flag")]
		[ExchangeField(MessageFields.MessageFlag)]
		public string MessageFlag { get; set; }

		/// <summary>
		/// The message priority.
		/// </summary>
		[ExchangeField(MessageFields.Priority)]
		public int Priority { get; set; }

		/// <summary>
		/// The message subject.
		/// </summary>
		[ExchangeField(MessageFields.Subject)]
		public override string Subject
		{
			get { return (!string.IsNullOrEmpty(subject)) ? subject : "(no subject)"; }
			set { subject = (value != null) ? value.Trim() : null; }
		}

		/// <summary>
		/// The message normalized subject.
		/// </summary>
		[ExchangeField(MessageFields.NormalizedSubject)]
		public string NormalizedSubject
		{
			get { return (!string.IsNullOrEmpty(normalizedSubject)) ? normalizedSubject : "(no subject)"; }
			set { normalizedSubject = (value != null) ? value.Trim() : null; }
		}

		/// <summary>
		/// Whether or not this message is flagged.
		/// </summary>
		public bool IsFlagged
		{
			get { return !string.IsNullOrEmpty(MessageFlag); }
		}

		/// <summary>
		/// Path to the icon.
		/// </summary>
		public override string IconPath
		{
			get { return (Read) ? "~/Images/icon-msg-read.gif" : "~/Images/icon-msg-unread.gif"; }
		}
	}
}